package com.no1.musicmgr.service;


import com.no1.musicmgr.mapper.UserAnalysisMapper;
import com.no1.musicmgr.vo.UserVo;
import lombok.Data;
import lombok.extern.log4j.Log4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
@Log4j
public class UserAnalysisService {
    @Autowired
    private UserAnalysisMapper userAnalysisMapper;

    public HashMap<String, Object> getUserAnalysisData() {
        HashMap<String, Object> map = new HashMap<>();


        // 用户性别
        map.put("user_sex", userAnalysisMapper.getUserDataBySex());

        // 假设 UserVo 中有表示人数的字段 count 和表示月份的字段 month

        // 1. 获取总用户数
        ArrayList<UserVo> personData = userAnalysisMapper.getPersonData();
        int totalUserCount = 0;
        if (!personData.isEmpty()) {
            // 假设 personData 只有一条记录，包含了所有用户的总数
            totalUserCount = personData.get(0).getCount();
        }
        map.put("total_person", totalUserCount);
        // 2. 获取每月的活跃人数
        ArrayList<UserVo> perMonth = userAnalysisMapper.getLoginCountPerMonth();

        // 初始化最大活跃人数和对应的月份
        Integer maxActiveCount = 0;
        String maxActiveMonth = "";
        Double maxActiveRatio = 0.0;
        String maxActiveRatiopercent = "";
        // 初始化每年的活跃人数总数
        Integer totalActivePerson = 0;

        // 3. 计算每月活跃用户比例
        for (UserVo userVo : perMonth) {
            int monthlyActiveCount = userVo.getPersonActive();
            double ratio = (double) monthlyActiveCount / totalUserCount;
            String percent = String.format("%.2f", ratio * 100) + "%";
            userVo.setActiveRatioPercent(percent);
            userVo.setScale(ratio);
            userVo.setActiveRatio(ratio);
            totalActivePerson += monthlyActiveCount;
            // 检查当前月份是否是活跃人数最多的
            if (monthlyActiveCount > maxActiveCount) {
                maxActiveCount = monthlyActiveCount;
                maxActiveMonth = userVo.getMonth(); // 假设 UserVo 有一个字段 month 存储月份
                maxActiveRatio = userVo.getScale();
                maxActiveRatiopercent = userVo.getActiveRatioPercent();
            }
        }
        // 4.存储每月的活跃人数和活跃比
        map.put("person_month", perMonth);

        // 5.存储最多活跃人数的月份和对应的活跃人数
        UserVo maxActive = new UserVo();
        maxActive.setMonth(maxActiveMonth);
        maxActive.setMaxActiveCount(maxActiveCount);
        maxActive.setScale(maxActiveRatio);
        maxActive.setActiveRatio(maxActiveCount);
        maxActive.setActiveRatioPercent(maxActiveRatiopercent);
        map.put("max_active", maxActive);

        // 每年的活跃总人数
        map.put("total_active_person", userAnalysisMapper.getTotalActivePersonInYearForStatusOne());
//        map.put("total_active_person", totalActivePerson);

        // 去年的总活跃数
        map.put("total_active_person_last_year", userAnalysisMapper.getTotalActivePersonInYearForStatusOne());

        // 打印或使用 map
//        map.forEach((key, value) -> log.info("Key: " + key + ", Value: " + value));
        map.forEach((key, value) -> log.info(String.format("Key: %s, Value: %s", key, value)));

        return map;
    }
}
